using UnityEngine;

public class PlayerDashState : PlayerAbilityState
{
    private Vector2 _dashDirection;
    private Vector2 _dashDirectionInput;
    private bool _dashInputStop;
    private bool _isHolding;

    private float _lastDashTime;

    private Vector2 _lastImagePos;

    public PlayerDashState(Player player, PlayerSM stateMachine, PlayerData playerData, string animBoolName, Core core)
        : base(player, stateMachine, playerData, animBoolName, core)
    {
    }

    public bool CanDash { get; private set; }

    public override void Enter()
    {
        base.Enter();

        CanDash = false;
        LightInputManager.Instance.UseDashInput();
        _player._dashDirectionIndicatior.gameObject.SetActive(true);

        _isHolding = true;
        _dashDirection = Vector2.right * _core.Movement.FacingDirection;

        Time.timeScale = _playerData._holdTimeScale;
        _startTime = Time.unscaledTime;
    }

    public override void Exit()
    {
        base.Exit();

        if (_core.Movement.CurrentVelocity.y > 0)
            _core.Movement.SetVelocityY(_core.Movement.CurrentVelocity.y * _playerData._dashEndYMultiplier);
    }

    public override void LogicUpdate()
    {
        base.LogicUpdate();

        if (!_isExitingState)
        {
            _dashInputStop = LightInputManager.Instance.DashInputStop;
            if (_isHolding)
            {
                _dashDirectionInput = LightInputManager.Instance.DashDirectionInput;

                if (_dashDirectionInput != Vector2.zero)
                {
                    _dashDirection = _dashDirectionInput;
                    _dashDirection.Normalize();
                }

                var angle = Vector2.SignedAngle(Vector2.right, _dashDirection);
                _player._dashDirectionIndicatior.rotation = Quaternion.Euler(0f, 0f, angle);

                if (_dashInputStop || Time.unscaledTime >= _startTime + _playerData._maxHoldTime)
                {
                    _isHolding = false;
                    Time.timeScale = 1f;
                    _startTime = Time.time;
                    _core.Movement.CheckIfShouldFilp(Mathf.RoundToInt(_dashDirection.x));
                    _player._rb.drag = _playerData._drag;
                    _core.Movement.SetVelocity(_playerData._dashVelocity, _dashDirection);
                    _player._dashDirectionIndicatior.gameObject.SetActive(false);
                    PlaceAfterImage();
                }
            }
            else
            {
                _core.Movement.SetVelocity(_playerData._dashVelocity, _dashDirection);
                CheckIfShouldPlaceAfterImage();
                if (Time.time > _startTime + _playerData._dashTime)
                {
                    _player._rb.drag = 0f;
                    _isAbilityDone = true;
                    _lastDashTime = Time.time;
                }
            }
        }
    }

    private void CheckIfShouldPlaceAfterImage()
    {
        if (Vector2.Distance(_player.transform.position, _lastImagePos) >= _playerData._distBetweenAfterImages)
            PlaceAfterImage();
    }

    private void PlaceAfterImage()
    {
        //MARKTR:��������ߵ�����һ������
        PlayerAfterImagePool.Instance.GetFromPool();
        _lastImagePos = _player.transform.position;
    }

    public bool CheckIfCanDash()
    {
        return CanDash && Time.time > _lastDashTime + _playerData._dashCoolDowm;
    }

    public void ResetCanDash()
    {
        CanDash = true;
    }
}